package com.fs.zise;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import Tools.Animation;
import Tools.AnimationData;
import Tools.AnimationListener;
import Tools.BitmapTool;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class GameMap implements AnimationListener {

	private  final  short map_w = 10; // 宽10
	private  final  short map_h = 12; // 高12
	private  final  short cell_width = 48;
	private  final  short cell_height = 48;
	private short map_X = 0;
	private short map_Y = 0;
	

	private static GameMap gameMap;
	public static GameMap getSingleInstance()
	{
		if(gameMap == null)
			gameMap = new GameMap();
		return gameMap;
	}
	
	/**
	 * 18---炸弹 28--激光发射器 38--镜子
	 * 
	 * 86--扳手 85---锤子
	 * 
	 * 84--火柴  83---水桶 
	 * 
	 * 50--過關元素
	 */
//	public static int zise0_map0_one[][][] = {
//		{
//			// 0   1  2   3   4   5   6   7   8   9 列
//			{ 30, 30, 86, 84, 1, 18, 30, 30, 30, 30 }, // 0 行
//			{ 30, 30, 30, 30, 1, 30, 30, 30, 30, 30 }, // 1行
//			{ 30, 30, 30, 30, 1, 18, 30, 30, 30, 30 }, // 2行
//			{ 18, 30, 30, 30, 1, 30, 30, 30, 30, 30 }, // 3行
//			{ 2,  2,  2,  18, 18, 30, 30, 30, 30, 30 }, // 4行
//			{ 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 }, // 5行
//			{ 28, 30, 30, 30, 38, 38, 30, 30, 30, 28 }, // 6行
//			{ 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 }, // 7行
//			{ 30, 30, 30, 30, 30, 30, 30,  2,  2,  2 }, // 8行
//			{ 30, 30, 30, 30, 30, 1, 30, 30, 30, 30 }, // 9行
//			{ 30, 30, 30, 30, 30, 1, 30, 30, 30, 30 }, // 10行
//			{ 30, 30, 30, 30, 30, 1, 30, 30, 30, 50 } // 11行
//		},
//			{},
//			{},
//	};
	
	
	public  void SetNewElmentInMap(int row ,int col,int vaule)
	{
		zise0_map0_one[row][col] = vaule;
	}

	public static final int MIRROR_DIRECTION_XIE_1TH = 101;
	public static final int MIRROR_DIRECTION_XIE_2TH = 102;

	// 镜子方向
//	public int zise0_map0_mirror_direction[][] = {
//			{ MIRROR_DIRECTION_XIE_1TH,// "\"
//				MIRROR_DIRECTION_XIE_2TH,// "\"
//			},
//			{}, 
//			{}
//
//	};
	
	public static final int TOOL_LASER_UP = 14;
	public static final int TOOL_LASER_RIGHT = 15;
	public static final int TOOL_LASER_DOWN = 16;
	public static final int TOOL_LASER_LEFT = 17;

	// 发射器方向
//	public int zise0_map0_laserEmitter_direction[][] = {
//			{  TOOL_LASER_RIGHT,TOOL_LASER_LEFT}, 
//			{}, 
//			{} 
//			};
	
	
	//過觀點的動畫
	private Bitmap targetBitmap = null;
	private Animation target_Animation = null;
	
	
	public void DrawMap(Canvas canvas, Paint paint, Bitmap bitmap,
			int[][] map_Id) {
		for (short i = 0; i < map_Id.length; i++) {
			for (short j = 0; j < map_Id[0].length; j++) {
				// 用草地铺满整个底层
					BitmapTool.drawClipImage(canvas, paint, bitmap, map_X
							+ getCellWidth() * j, map_Y + getCellHeight() * i,
							5 * getCellWidth(), 3 * getCellHeight(), getCellWidth(),
							getCellHeight());
				if (map_Id[i][j] == 1 || map_Id[i][j] == 2)
				{
					// 根据ID算　障碍物 所在 图片上的　行和列
					int rowID = (map_Id[i][j] - 1) / 8;
					int colID = (map_Id[i][j] - 1) % 8;
					BitmapTool.drawClipImage(canvas, paint, bitmap, map_X
							+ getCellWidth() * j, map_Y + getCellHeight() * i, colID
							* getCellWidth(), rowID * getCellHeight(), getCellWidth(),
							getCellHeight());
				}
				else if(map_Id [i][j] == 50)
				{
					//过关的节点坐标 //图片上的 第7列 第3行
//					BitmapTool.drawClipImage(canvas, paint, bitmap, map_X
//							+ getCellWidth() * j, map_Y + getCellHeight() * i, 7
//							* getCellWidth(), 2 * getCellHeight(), getCellWidth(),
//							getCellHeight());
					target_Animation.paint(canvas, paint, map_X+ getCellWidth() * j+getCellWidth()/2, map_Y + getCellHeight() * i+getCellHeight()/2);
					target_Animation.update(true);
				}
			}
		}
	}
	

	public  boolean isNodCanPass(int row, int col) {
		return (zise0_map0_one[row][col] == 30 || (zise0_map0_one[row][col] == 86)
				|| (zise0_map0_one[row][col] == 85)|| (zise0_map0_one[row][col] == 84)
						|| (zise0_map0_one[row][col] == 83)||(zise0_map0_one[row][col] == 50));
	}


	public  short getLevel() {
		return level;
	}


	public  short getMapW() {
		return map_w;
	}


	public  short getMapH() {
		return map_h;
	}


	public  short getCellWidth() {
		return cell_width;
	}


	public  short getCellHeight() {
		return cell_height;
	}
	
	private final String MAP_DATA_PATH ="mapdata/";
	private byte level = 0; //关卡信息
	private static byte [] stageMapData = new byte[120];//从文件里读取地图数据
	private static byte [] stage_mirrorDir_Data = new byte[2];//从文件里读取的镜子的方向数据(1个镜子一个方向)
	private static byte [] stage_laserEmitterDir_Data = new byte[2]; //从文件里读取的激光发射器方向(1个发射器一个方向)
	
	public int zise0_map0_one[][] = new int[12][10];//地图数据
	public int zise0_map0_mirror_direction [] = new int[2]; //镜子方向
	public int zise0_map0_laserEmitter_direction []= new int[2];//发射器方向
	
	public void loadMapData(String filename)
	{
		//只執行一次
		if(targetBitmap == null)
			targetBitmap = BitmapTool.getImageFromAssetFile("targetAni.png");
		if (target_Animation == null)
		{
			target_Animation = new Animation(new AnimationData("targetAni.sprite",targetBitmap));
			target_Animation.setAction(0);
		}
		
		
		readFile(filename);
		
		for (int i = 0; i < map_h; i++) {//行
			for (int j = 0; j < map_w; j++) {//列
				zise0_map0_one [i][j] = stageMapData[10*i+j];
			}
		}
		
		for (int i = 0; i < 2; i++) {
			zise0_map0_mirror_direction[i]=stage_mirrorDir_Data[i];
		}
		
		for (int i = 0; i < 2; i++) {
			zise0_map0_laserEmitter_direction[i] =stage_laserEmitterDir_Data[i];
		}
		
	}
	public  void readFile(String filename)
	{
//		File file = new File("E:\\androidworkspaceII\\MyZiSeJingLing\\assets\\mapdata\\stage_one.dat");
//		if(!file.exists()||file.isDirectory())
//			return ;
		
		AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
		InputStream is = null;
		try {
			is = assetManager.open(MAP_DATA_PATH+filename);
			is.read(stageMapData);
			is.read(stage_mirrorDir_Data);
			is.read(stage_laserEmitterDir_Data);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		} 
//		FileInputStream fis = null;
//		DataInputStream dis = null;
//		try {
//			fis = new FileInputStream(file);
//			dis = new DataInputStream(fis);
			
//			dis.read(stageMapData);
				
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		}
		
		 finally
			{
				try {
					is.close();
//					dis.close();
//					fis.close();
				} catch (Exception e) {
				}
			}
	}

	@Override
	public void endofAnimation(Animation animation) {
		// TODO Auto-generated method stub
		System.out.println("GameMap.endofAnimation()");
	}

}
